{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8a937a74",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.tools.measurespace import MeasureSpaceToolSpec\n",
    "from llama_index.core.agent.workflow import FunctionAgent\n",
    "from llama_index.llms.openai import OpenAI\n",
    "from dotenv import load_dotenv\n",
    "import os\n",
    "\n",
    "load_dotenv()\n",
    "\n",
    "api_keys = {\n",
    "    'hourly_weather': os.getenv('HOURLY_WEATHER_API_KEY'),\n",
    "    'daily_weather': os.getenv('DAILY_WEATHER_API_KEY'),\n",
    "    'daily_climate': os.getenv('DAILY_CLIMATE_API_KEY'),\n",
    "    'air_quality': os.getenv('AIR_QUALITY_API_KEY'),\n",
    "    'geocoding': os.getenv('GEOCODING_API_KEY'),\n",
    "}\n",
    "\n",
    "tool_spec = MeasureSpaceToolSpec(api_keys)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "39c37351",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "get_hourly_weather_forecast\n",
      "get_daily_weather_forecast\n",
      "get_daily_climate_forecast\n",
      "get_daily_air_quality_forecast\n",
      "get_latitude_longitude_from_location\n",
      "get_location_from_latitude_longitude\n"
     ]
    }
   ],
   "source": [
    "for tool in tool_spec.to_tool_list():\n",
    "    print(tool.metadata.name)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a61e1618",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[Document(id_='dfe2020a-f0e8-4625-b5a7-265fd9aaf8f5', embedding=None, metadata={'Daily weather for location': 'New York', 'Date': '2025-05-30'}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={}, metadata_template='{key}: {value}', metadata_separator='\\n', text_resource=MediaResource(embeddings=None, data=None, text='total precipitation: 18.81 mm,daily minimum temperature: 16.06 C,daily maximum temperature: 25.05 C', path=None, url=None, mimetype=None), image_resource=None, audio_resource=None, video_resource=None, text_template='{metadata_str}\\n\\n{content}'),\n",
       " Document(id_='89fb5a5f-03f4-463a-ab8c-b0d3eb5cc556', embedding=None, metadata={'Daily weather for location': 'New York', 'Date': '2025-05-31'}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={}, metadata_template='{key}: {value}', metadata_separator='\\n', text_resource=MediaResource(embeddings=None, data=None, text='total precipitation: 104.25 mm,daily minimum temperature: 13.16 C,daily maximum temperature: 20.74 C', path=None, url=None, mimetype=None), image_resource=None, audio_resource=None, video_resource=None, text_template='{metadata_str}\\n\\n{content}'),\n",
       " Document(id_='cb4d21d8-dadb-40c0-a73d-5934f22820e8', embedding=None, metadata={'Daily weather for location': 'New York', 'Date': '2025-06-01'}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={}, metadata_template='{key}: {value}', metadata_separator='\\n', text_resource=MediaResource(embeddings=None, data=None, text='total precipitation: 0.0 mm,daily minimum temperature: 11.83 C,daily maximum temperature: 19.86 C', path=None, url=None, mimetype=None), image_resource=None, audio_resource=None, video_resource=None, text_template='{metadata_str}\\n\\n{content}'),\n",
       " Document(id_='d23e486a-6af9-4b61-a469-aeab2cfc8bea', embedding=None, metadata={'Daily weather for location': 'New York', 'Date': '2025-06-02'}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={}, metadata_template='{key}: {value}', metadata_separator='\\n', text_resource=MediaResource(embeddings=None, data=None, text='total precipitation: 0.25 mm,daily minimum temperature: 12.08 C,daily maximum temperature: 23.35 C', path=None, url=None, mimetype=None), image_resource=None, audio_resource=None, video_resource=None, text_template='{metadata_str}\\n\\n{content}'),\n",
       " Document(id_='d0818797-81a6-46fe-863e-d0ae0cf22f13', embedding=None, metadata={'Daily weather for location': 'New York', 'Date': '2025-06-03'}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={}, metadata_template='{key}: {value}', metadata_separator='\\n', text_resource=MediaResource(embeddings=None, data=None, text='total precipitation: 1.19 mm,daily minimum temperature: 15.05 C,daily maximum temperature: 28.45 C', path=None, url=None, mimetype=None), image_resource=None, audio_resource=None, video_resource=None, text_template='{metadata_str}\\n\\n{content}'),\n",
       " Document(id_='893b5272-6a59-49e6-806d-de655cd10c72', embedding=None, metadata={'Daily weather for location': 'New York', 'Date': '2025-06-04'}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={}, metadata_template='{key}: {value}', metadata_separator='\\n', text_resource=MediaResource(embeddings=None, data=None, text='total precipitation: 0.0 mm,daily minimum temperature: 19.89 C,daily maximum temperature: 32.15 C', path=None, url=None, mimetype=None), image_resource=None, audio_resource=None, video_resource=None, text_template='{metadata_str}\\n\\n{content}'),\n",
       " Document(id_='5d8b77c6-d0df-4291-8fdb-b1c6085f6c3d', embedding=None, metadata={'Daily weather for location': 'New York', 'Date': '2025-06-05'}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={}, metadata_template='{key}: {value}', metadata_separator='\\n', text_resource=MediaResource(embeddings=None, data=None, text='total precipitation: 0.25 mm,daily minimum temperature: 21.11 C,daily maximum temperature: 30.47 C', path=None, url=None, mimetype=None), image_resource=None, audio_resource=None, video_resource=None, text_template='{metadata_str}\\n\\n{content}'),\n",
       " Document(id_='b718fb88-29cb-45b1-9039-7549750b73e3', embedding=None, metadata={'Daily weather for location': 'New York', 'Date': '2025-06-06'}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={}, metadata_template='{key}: {value}', metadata_separator='\\n', text_resource=MediaResource(embeddings=None, data=None, text='total precipitation: 0.44 mm,daily minimum temperature: 18.31 C,daily maximum temperature: 23.11 C', path=None, url=None, mimetype=None), image_resource=None, audio_resource=None, video_resource=None, text_template='{metadata_str}\\n\\n{content}'),\n",
       " Document(id_='5ebc2b10-6ec0-4e08-bbd5-108fb36064a0', embedding=None, metadata={'Daily weather for location': 'New York', 'Date': '2025-06-07'}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={}, metadata_template='{key}: {value}', metadata_separator='\\n', text_resource=MediaResource(embeddings=None, data=None, text='total precipitation: 14.19 mm,daily minimum temperature: 16.9 C,daily maximum temperature: 21.35 C', path=None, url=None, mimetype=None), image_resource=None, audio_resource=None, video_resource=None, text_template='{metadata_str}\\n\\n{content}'),\n",
       " Document(id_='ac4ae720-2246-4482-babd-9cc131eb9d17', embedding=None, metadata={'Daily weather for location': 'New York', 'Date': '2025-06-08'}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={}, metadata_template='{key}: {value}', metadata_separator='\\n', text_resource=MediaResource(embeddings=None, data=None, text='total precipitation: 2.0 mm,daily minimum temperature: 19.74 C,daily maximum temperature: 26.92 C', path=None, url=None, mimetype=None), image_resource=None, audio_resource=None, video_resource=None, text_template='{metadata_str}\\n\\n{content}'),\n",
       " Document(id_='ef2a553f-edb3-4398-bd79-09777a43da93', embedding=None, metadata={'Daily weather for location': 'New York', 'Date': '2025-06-09'}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={}, metadata_template='{key}: {value}', metadata_separator='\\n', text_resource=MediaResource(embeddings=None, data=None, text='total precipitation: 0.12 mm,daily minimum temperature: 20.44 C,daily maximum temperature: 29.53 C', path=None, url=None, mimetype=None), image_resource=None, audio_resource=None, video_resource=None, text_template='{metadata_str}\\n\\n{content}'),\n",
       " Document(id_='b7d01619-0956-40f6-9336-075220132a2f', embedding=None, metadata={'Daily weather for location': 'New York', 'Date': '2025-06-10'}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={}, metadata_template='{key}: {value}', metadata_separator='\\n', text_resource=MediaResource(embeddings=None, data=None, text='total precipitation: 4.62 mm,daily minimum temperature: 22.0 C,daily maximum temperature: 31.05 C', path=None, url=None, mimetype=None), image_resource=None, audio_resource=None, video_resource=None, text_template='{metadata_str}\\n\\n{content}'),\n",
       " Document(id_='1aefe45c-04ae-47aa-8d89-2add1ab22afb', embedding=None, metadata={'Daily weather for location': 'New York', 'Date': '2025-06-11'}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={}, metadata_template='{key}: {value}', metadata_separator='\\n', text_resource=MediaResource(embeddings=None, data=None, text='total precipitation: 27.81 mm,daily minimum temperature: 20.97 C,daily maximum temperature: 26.78 C', path=None, url=None, mimetype=None), image_resource=None, audio_resource=None, video_resource=None, text_template='{metadata_str}\\n\\n{content}'),\n",
       " Document(id_='4ecd2db3-f0f4-4821-9496-7dd06a0b4915', embedding=None, metadata={'Daily weather for location': 'New York', 'Date': '2025-06-12'}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={}, metadata_template='{key}: {value}', metadata_separator='\\n', text_resource=MediaResource(embeddings=None, data=None, text='total precipitation: 3.12 mm,daily minimum temperature: 21.89 C,daily maximum temperature: 26.22 C', path=None, url=None, mimetype=None), image_resource=None, audio_resource=None, video_resource=None, text_template='{metadata_str}\\n\\n{content}'),\n",
       " Document(id_='0fe23a8c-da07-4f1f-9002-a6529f0c3487', embedding=None, metadata={'Daily weather for location': 'New York', 'Date': '2025-06-13'}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={}, metadata_template='{key}: {value}', metadata_separator='\\n', text_resource=MediaResource(embeddings=None, data=None, text='total precipitation: 1.19 mm,daily minimum temperature: 21.6 C,daily maximum temperature: 28.62 C', path=None, url=None, mimetype=None), image_resource=None, audio_resource=None, video_resource=None, text_template='{metadata_str}\\n\\n{content}')]"
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tool_spec.get_daily_weather_forecast('New York')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a6b1429e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[Document(id_='6b841c56-a3a8-45ff-ab7f-c8c02d4392e7', embedding=None, metadata={'Latitude and longitude for location': 'New York'}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={}, metadata_template='{key}: {value}', metadata_separator='\\n', text_resource=MediaResource(embeddings=None, data=None, text='latitude: 40.714, longitude: -74.006', path=None, url=None, mimetype=None), image_resource=None, audio_resource=None, video_resource=None, text_template='{metadata_str}\\n\\n{content}')]"
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tool_spec.get_latitude_longitude_from_location('New York')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9798ff03",
   "metadata": {},
   "outputs": [],
   "source": [
    "agent = FunctionAgent(\n",
    "    tools=tool_spec.to_tool_list(),\n",
    "    llm=OpenAI(model=\"gpt-4.1\"),\n",
    ")\n",
    "\n",
    "print(\n",
    "    await agent.run(\"How's the temperature for New York in next 3 days?\")\n",
    ")\n",
    "print(\n",
    "    await agent.run(\"What's the latitude and longitude of New York?\")\n",
    ")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
